import { Dimensions, PixelRatio } from 'react-native'

export default function handleContent(content) {
    const length = content.length
    let array = []
    let x = 0,
        y,
        m = 0
    while (x < length) {
        let _array = []
        for (let i = 0; i <= 16; i++) {
            let str_spa = content.substring(x, x + 1)
            let str_sto = content.substring(x, x + 18)
            const re = /^\s+$/
            if (str_sto.indexOf('”') != -1) {
                y = x + str_sto.indexOf('”') + 1
                _array[i] = content.substring(x, y)
                x = y
                continue
            }
            else if (str_sto.indexOf('。') != -1) {
                y = x + str_sto.indexOf('。') + 1
                if (re.exec(content.substring(y, y + 1))) {
                    y = x + str_sto.indexOf('。') + 1
                    _array[i] = content.substring(x, y)
                    x = y
                    continue
                }
                else {
                    if (str_sto.indexOf('！') != -1) {
                        y = x + str_sto.indexOf('！') + 1
                        _array[i] = content.substring(x, y)
                        x = y
                        continue
                    }
                    else {
                        y = x + 18
                        _array[i] = content.substring(x, y)
                        x = y
                        continue
                    }
                }
            }
            else if (str_sto.indexOf('！') != -1) {
                y = x + str_sto.indexOf('！') + 1
                if (re.exec(content.substring(y, y + 1))) {
                    y = x + str_sto.indexOf('！') + 1
                    _array[i] = content.substring(x, y)
                    x = y
                    continue
                }
                else {
                    y = x + 18
                    _array[i] = content.substring(x, y)
                    x = y
                    continue
                }
            }
            else if (str_sto.indexOf('？') != -1) {
                y = x + str_sto.indexOf('？') + 1
                if (re.exec(content.substring(y, y + 1))) {
                    y = x + str_sto.indexOf('？') + 1
                    _array[i] = content.substring(x, y)
                    x = y
                    continue
                }
                else {
                    y = x + 18
                    _array[i] = content.substring(x, y)
                    x = y
                    continue
                }
            }
            else if (re.exec(str_spa)) {
                y = x + 24
                if (content.substring(x, y).indexOf('。') != -1) {
                    y = x + content.substring(x, y).indexOf('。') + 1
                    _array[i] = content.substring(x, y)
                    x = y
                    continue
                }
                _array[i] = content.substring(x, y)
                x = y
                continue
            }
            else {
                y = x + 18
                _array[i] = content.substring(x, y)
                x = y
            }
        }
        array[m] = _array
        m++
    }
    // console.log((m - 1) * 375);
    return array
}

export let contentFormat = (content) => {
    let fontCount = parseInt(Dimensions.get('window').width / 18 - 1)
    let fontLines = parseInt((Dimensions.get('window').height - 100) / 34)
    const length = content.length
    let array = []
    let x = 0,
        y,
        m = 0
    while (x < length) {
        let _array = []
        for (let i = 0; i <= fontLines; i++) {
            let str = content.substring(x, x + fontCount)
            if (str.indexOf('@') !== -1) {
                y = x + str.indexOf('@') + 1
                _array[i] = content.substring(x, y).replace('@', '')
                x = y
                continue
            } else {
                y = x + fontCount
                _array[i] = content.substring(x, y)
                x = y
                continue
            }
        }
        array[m] = _array
        m++
    }
    return array
}

// 将一段文字分成若干页面;1.42857143
export const wordToPageArr = (wordText, fontSize) => {
    let win_width = parseInt(Dimensions.get('window').width, 0) - PixelRatio.roundToNearestPixel(30) // 屏幕宽度;
    let win_height = parseInt(Dimensions.get('window').height, 0) - PixelRatio.roundToNearestPixel(156) // 屏幕高度;
    // let line_height = parseInt(fontSize) * 1.42857143; //行高;
    let line_height = fontSize * 1.5 // 行高;

    let line_num = Math.floor(win_height / line_height) // 一页最大行数;
    let line_text_num = Math.floor(win_width / (fontSize + 3)) // 一行最大字数;
    let wordTextArr = wordText.split('\n')
    // let wordTextArrLineNum=[];
    let wordPageArr = [{
        text: '',
        lineNum: 0
    }]
    wordTextArr.map((item, index) => {
        let lineNum = Math.ceil(item.length / line_text_num)
        // wordTextArrLineNum.push(lineNum);
        pushTextToArr(wordPageArr, item, lineNum, line_num, line_text_num)
    })
    return wordPageArr
}

function pushTextToArr(arr, text, lineNum, lineMaxNum, lineTextNum) {
    if (arr[arr.length - 1].lineNum + lineNum < lineMaxNum) {
        arr[arr.length - 1].text = arr[arr.length - 1].text + '\n' + text
        arr[arr.length - 1].lineNum = arr[arr.length - 1].lineNum + lineNum
    } else {
        let newLineNum = arr[arr.length - 1].lineNum + lineNum
        let hisNum = arr[arr.length - 1].lineNum
        let pageNum = Math.ceil(newLineNum / lineMaxNum)
        let endNum
        let endPageNum
        for (let i = 0; i < pageNum; i++) {
            if (i === 0) {
                arr[arr.length - 1].text = arr[arr.length - 1].text + '\n' + text.substring(0, ((i + 1) * lineMaxNum - hisNum) * lineTextNum)
                arr[arr.length - 1].lineNum = lineMaxNum
                arr.push({
                    text: '',
                    lineNum: 0
                })
            } else {
                if ((i + 1) * lineMaxNum - hisNum > lineNum) {
                    endNum = lineNum
                    endPageNum = newLineNum % lineMaxNum
                } else {
                    endNum = (i + 1) * lineMaxNum - hisNum
                    endPageNum = lineMaxNum
                }
                arr[arr.length - 1].text = arr[arr.length - 1].text + text.substring((i * lineMaxNum - hisNum) * lineTextNum, endNum * lineTextNum)
                arr[arr.length - 1].lineNum = endPageNum
                if (endPageNum === lineMaxNum) {
                    arr.push({
                        text: '',
                        lineNum: 0
                    })
                }
            }
        }
    }
}

// 将一段文字分成若干页面;1.42857143
export const wordToPageArr2 = (wordText, fontSize) => {
    let win_width = parseInt(Dimensions.get('window').width, 0) - PixelRatio.roundToNearestPixel(30) // 屏幕宽度;
    let win_height = parseInt(Dimensions.get('window').height, 0) - PixelRatio.roundToNearestPixel(156) // 屏幕高度;
    // let line_height = parseInt(fontSize) * 1.42857143; //行高;
    let line_height = fontSize * 1.5 // 行高;

    let line_num = Math.floor(win_height / line_height) // 一页最大行数;
    let line_text_num = Math.floor(win_width / (fontSize + 3)) // 一行最大字数;
    let wordTextArr = wordText.split('\n')
    let wordPageArr = []
    let texts = []
    wordTextArr.map((item, index) => {
        let itemCoay = '\t\t' + item
        let lineNum = Math.ceil(itemCoay.length / line_text_num)
        for (let i = 0; i < lineNum; i++) {
            let end = (i + 1) * line_text_num
            if (i === (lineNum - 1)) {
                end = itemCoay.length - 1
            }
            let line = itemCoay.substring(i * line_text_num, end)
            if (texts.length === line_num) {
                wordPageArr.push({
                    texts: texts
                })
                texts = []
            }
            texts.push(line)
        }
    })
    return wordPageArr
}
